package com.xhs.controller;

import com.xhs.dto.request.UserRequest;
import com.xhs.dto.response.PicCaptchaResponse;
import com.xhs.message.Result;
import com.xhs.message.ReturnResult;
import com.xhs.service.DevMonitorService;
import com.xhs.service.ToolsService;
import com.xhs.utils.CaptchaUtil;
import com.xhs.utils.CommonAvatarUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @desc: 工具类接口
 * @projectName: tools-parent
 * @author: xhs
 * @date: 2022/12/3 23:05
 * @version: JDK 1.8
 */
@RestController
@RequestMapping("/tools")
public class ToolsController {
    @Resource
    private DevMonitorService monitorService;
    @Resource
    private CaptchaUtil captchaUtil;
    @Resource
    private ToolsService toolsService;

    /**
     * 生成验证码
     *
     * @return ReturnResult<Object>
     */
    @GetMapping("/getCode")
    public ReturnResult<Object> getByte() {
        Map<String, String> map = captchaUtil.getPicCaptcha();
        String validCodeReqNo = map.get("validCodeReqNo");
        String validCodeBase64 = map.get("validCodeBase64");
        PicCaptchaResponse response = PicCaptchaResponse.builder()
                .validCodeReqNo(validCodeReqNo)
                .validCodeBase64(validCodeBase64)
                .build();
        return ReturnResult.success(response);
    }

    /**
     * 通用头像工具类，生成文字头像
     *
     * @param name name
     * @return ReturnResult<Object>
     */
    @GetMapping("/avatar")
    public ReturnResult<Object> setAvatar(String name) {
        String img = CommonAvatarUtil.generateImg(name);
        return ReturnResult.build(Result.SUCCESS).setData(img);
    }

    /**
     * 获取服务器监控信息
     *
     * @return ReturnResult<Object>
     */
    @GetMapping("/getMonitorInfo")
    public ReturnResult<Object> getMonitorInfo() {
        return monitorService.serverInfo();
    }

    /**
     * 获取IP信息等
     *
     * @return ReturnResult<Object>
     */
    @GetMapping("/getIpInfo")
    public ReturnResult<Object> getIpInfo() {
        return monitorService.getIpInfo();
    }

    /**
     * 获取用户信息，参数需要签名
     *
     * @param params 用户名
     * @return ReturnResult<Object>
     */
    @PostMapping("/getUser")
    public ReturnResult<Object> getUser(@Validated @RequestBody UserRequest params) {
        return toolsService.getUser(params);
    }

    /**
     * 获取用户信息，参数需要签名
     *
     * @param name 用户名
     * @return ReturnResult<Object>
     */
    @GetMapping("/getName")
    public ReturnResult<Object> getName(String name) {
        return toolsService.getName(name);
    }
}
